package com.lyh.controller;

import com.lyh.utils.CaptchaUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class PageController {

    @RequestMapping(value = {"/","/index","/index.html","/home","/index.page"})
    String index(){
        return "index";
    }

    @RequestMapping("/login")
    String loginPage(){
        return "login";
    }

    @RequestMapping("/register")
    String registerPage(){
        return "register";
    }

    @RequestMapping("/guide")
    String guidePage(){
        return "guide";
    }

    /**
     * 展示帮助信息界面
     */
    @RequestMapping("/guide/{type}")
    String changeInfo(@PathVariable("type")String type){
        //用于信息页面ajax的碎片切换
        String prefix = "components/public/info::";
        switch (type){
            case "ticket": return prefix+"ticket-info";
            case "refund": return prefix+"refund-info";
            case "service": return prefix+"service-info";
            default: return prefix+"ticket-info";
        }
    }

    /**
     * 首页的搜索跳转到票页面
     */
    @RequestMapping("/ticket")
    String ticketPage(String departure_city, String arrival_city, String departure_time, String only_high_speed,Model model){
        //请求参数, 放入model中
        //再ticket页面获取这些参数, 然后使用ajax来请求票的数据
        model.addAttribute("from",departure_city);
        model.addAttribute("to",arrival_city);
        model.addAttribute("date",departure_time);
        model.addAttribute("highSpeed",only_high_speed);
        //将这些参数传到页面中, 页面再通过onload函数ajax请求票数据
        return "ticket";
    }

    //跳转到登陆界面, 并且在登陆框上显示相应的提示信息
    //如果没有写什么通知类型, 就显示原页面
    @GetMapping("/haveToLogin/{tipType}")
    String haveToLogin(Model model,@PathVariable String tipType){
        if ("1".equals(tipType))
            model.addAttribute("tip",1);//提示没有权限请登陆
        else if("2".equals(tipType))
            model.addAttribute("tip",2);//提示已经注册完毕, 请登陆
        return "login";
    }

    //获取验证码
    @RequestMapping("/public/getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String code = CaptchaUtil.outputVerifyImage(150, 60, response.getOutputStream(), 4);
        // 注册的时候, 就对比是否正确
        session.setAttribute("code",code);//将验证码存入session
    }



    /**
     * 显示通知页面
     * @param msg 显示的信息
     * @param status 显示字体的颜色
     * @param url 自动跳转的路径
     */
    @RequestMapping("/msg")
    public String msgPage(String msg, int status, String url, Model model){
        model.addAttribute("status",status);
        model.addAttribute("msg",msg);
        model.addAttribute("url",url);
        return "msg";
    }




    //测试页面
    @RequestMapping("/test")
    public String testPage(Model model){
        //return msgPage("跳转到首页",2,"http:localhost:8080/index",model);
        return "test";
    }
    /**
     * 邮件激活 (没有使用)
     * 原本打算往邮箱中发送跳转邮箱激活页面链接
     * @param username 用户名
     * @param active 激活码
     */
    @RequestMapping("/activeMail/{username}/{active}")
    public String mailPage(@PathVariable("username") String username, @PathVariable("active") String active, Model model){
        model.addAttribute("username",username);
        model.addAttribute("active",active);
        return "activeMail";
    }

}
